package com.uxbee.service;

import java.util.ArrayList;
import java.util.List;

import com.uxbee.db.ConnectionManager;
import com.uxbee.pojo.HtmlData;

public class ObtainHtml {

	public static int cu = 0;
	
	public static void obtainHtml(HtmlData htmlData, int width, int height) {
		
		// 设置宽度
		LinkParser.width = width;
		// 设置深度
		LinkParser.height = height;
		
		// 初始化链接
		LinkLoad.resHtmlDataList = new ArrayList<HtmlData>();
		
		// 获取整站的站内链接
		LinkLoad.load(htmlData, new LinkFilter() {

			public boolean accept(String mainUrl, String url) {
				int index = url.indexOf(mainUrl);
				if (index == -1) {
					return false;
				}
				else {
					return true;
				}
			}

		});

		// 获取网站链接
		List<HtmlData> htmlDataList = LinkLoad.resHtmlDataList;

		// 获取整站的文本内容，并保存数据
		if (htmlDataList != null && htmlDataList.size() > 0) {
			System.out.println(htmlDataList.size() + "条连接");
			try {
				for (int i = 0; i < htmlDataList.size(); i++) {
					HtmlData hd = htmlDataList.get(i);
					String content = GetContent.getContentUsingParser(hd
							.getUrl());
					/*if (content == null || content.length() == 0) {
						hd.setErrorCount(1);

					}
					else {
						hd.setContent(content);
					}
					hd.setTime(new Date());
					try {
						ConnectionManager.saveToDB(hd);
					}
					catch (Exception e) {
						System.out.println(e);
					}*/
					if (content.indexOf("网站") != -1){
						System.out.println("包含\"网站\"的连接数量" + ++cu);
					}else{
						htmlDataList.remove(i);
						i--;
					}
				}
			}
			catch (Exception e) {
				System.out.println(e);
			}
			finally {
				ConnectionManager.close();
			}
		}
	}
}
